package com.linkage.educloud.ah.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.linkage.educloud.ah.Constants;
import com.linkage.educloud.ah.MyApplication;
import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.bean.ClazzContact;
import com.linkage.educloud.ah.config.AccountConfig;
import com.linkage.educloud.ah.config.AppConfig;
import com.zzhou.common.library.util.StringUtil;
import com.zzhou.common.library.util.SystemTool;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <desc>
 * Created by The Moss on 2018/11/28.
 */

public class CallAdapter extends BaseAdapter {
    Context mContext;
    List<ClazzContact> members;
    OnCheckOpenPhoneListener mListener;
    public interface OnCheckOpenPhoneListener{
        void onCheck(boolean ischeck);
    }
    public void setListener(OnCheckOpenPhoneListener listener){
        mListener = listener;
    }
    public CallAdapter(Context mContext) {
        this.mContext = mContext;
        this.members = new ArrayList<>();
    }
    public void setDatas(List<ClazzContact> contacts){
        if (contacts == null) {
            return;
        }
        members.clear();
        members.addAll(contacts);
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        return members.size();
    }

    @Override
    public ClazzContact getItem(int position) {
        return members.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_contact_class, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.setData(position);
        return convertView;
    }

    public class ViewHolder {
        private TextView class_name;
        private TextView short_dn;
        private ImageView sms_shortdn;
        private ImageView phone_img;
        private TextView phone;
        private ImageView is_open;
        private TextView open_text;

        public ViewHolder(View itemView) {
            class_name = itemView.findViewById(R.id.class_name);
            short_dn = itemView.findViewById(R.id.short_dn);
            phone = itemView.findViewById(R.id.phone);
            open_text = itemView.findViewById(R.id.open_text);
            sms_shortdn = itemView.findViewById(R.id.sms_shortdn);
            is_open = itemView.findViewById(R.id.is_open);
            phone_img = itemView.findViewById(R.id.phone_img);
        }

        public void setData(final int position) {
            final ClazzContact member = members.get(position);
            if (member != null) {
                class_name.setText(member.getSchoolName()
                        + member.getClassName());
                if (StringUtil.isNullOrEmpty(member.short_dn)) {
                    short_dn.setText("未开通");
                    short_dn.setTextColor(Color.RED);
                } else {
                    short_dn.setText(member.short_dn);
                    short_dn.setTextColor(Color.BLACK);
                    int has_shortdn = AppConfig.getInstance().getInt(AppConfig.ShareKey.KEY_HAS_SHORTDN, 0);
                    String clazzid = AppConfig.getInstance().getString(AppConfig.ShareKey.KEY_SHORTDN_CLAZZS, "");
                    if (!StringUtil.isNullOrEmpty(clazzid)) {
                        String[] clazzs = clazzid.split(",");
                        List clazzids = Arrays.asList(clazzs);
                        if (SystemTool.YIDONG == SystemTool.getPhoneInfos(mContext)
                                && has_shortdn == Constants.VClazzType.Vclass
                                && clazzids.contains(member.classid)) {
                            phone_img.setVisibility(View.VISIBLE);
                            sms_shortdn.setVisibility(View.VISIBLE);
                        } else {
                            phone_img.setVisibility(View.GONE);
                            sms_shortdn.setVisibility(View.GONE);
                        }
                    } else {
                        phone_img.setVisibility(View.GONE);
                        sms_shortdn.setVisibility(View.GONE);
                    }

                }
                if (StringUtil.isNullOrEmpty(member.dn)) {
                    is_open.setVisibility(View.GONE);
                    open_text.setVisibility(View.GONE);
                } else {
                    is_open.setVisibility(View.VISIBLE);
                    open_text.setVisibility(View.VISIBLE);
                    phone.setTextColor(Color.BLACK);
                }
                if (member.isopen_phone == 0) {
                    open_text.setText("未公开");
                    open_text.setTextColor(Color.RED);
                    is_open.setImageResource(R.mipmap.icon_close_checked);
                    if (!StringUtil.isNullOrEmpty(member.dn) && member.dn.length() == 11) {
                        phone.setText(member.dn.substring(0, 3) + "****"
                                + member.getDn().substring(7, member.dn.length()));
                    } else {
                        phone.setText("");
                    }

                } else {
                    open_text.setText("已公开");
                    open_text.setTextColor(Color.BLACK);
                    phone.setText(member.dn);
                    is_open.setImageResource(R.mipmap.icon_open_checked);
                }
                phone_img.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        SystemTool.dailTel(mContext, member.getShort_dn());
                    }
                });
                sms_shortdn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        SystemTool.sendSMS(mContext, member.getShort_dn());
                    }
                });
                is_open.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (member.isopen_phone == 0) {
                            MyApplication.getInstance().getResovler().updateIsopen_phone(
                                    AccountConfig.getLoginAccount().getLoginAccount(),member.id,1,member.getClassid());
                            open_text.setText("公开");
                            open_text.setTextColor(Color.RED);
                            is_open.setImageResource(R.mipmap.icon_close_checked);
                            member.isopen_phone = 1;
                            notifyDataSetChanged();
                            if (!StringUtil.isNullOrEmpty(member.dn)&&member.dn.length()==11) {
                                phone.setText(member.dn.substring(0, 3) + "****"
                                        + member.getDn().substring(7, member.dn.length()));
                            }else{
                                phone.setText("");
                            }
                            if (mListener!=null){
                                mListener.onCheck(true);
                            }
                        }else{
                            MyApplication.getInstance().getResovler().updateIsopen_phone(
                                    AccountConfig.getLoginAccount().getLoginAccount(),member.id,0,member.getClassid());
                            phone.setText(member.dn);
                            open_text.setText("未公开");
                            open_text.setTextColor(Color.BLACK);
                            is_open.setImageResource(R.mipmap.icon_open_checked);
                            member.isopen_phone = 0;
                            notifyDataSetChanged();
                            if (mListener!=null){
                                mListener.onCheck(false);
                            }
                        }
                    }
                });
            } else {
                short_dn.setText("未开通");
                short_dn.setTextColor(Color.RED);
                phone.setText("未绑定");
                phone.setTextColor(Color.RED);
            }
        }
    }
}
